#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : ScrollBar.py
# Purpose  : 
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-07-09 09:34
# ==============================================================================

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QScrollBar, QWidget


class ScrollBar(QScrollBar):
    s_signal = pyqtSignal(float)

    def __init__(self, mom: QWidget, *args, **kwargs):
        super(ScrollBar, self).__init__(*args, **kwargs)
        self.mom = mom  # 滑块所在的编辑器
        self.setObjectName(f'{self.mom.objectName()}_myscrollbar')
        self.valueChanged.connect(self.slider_moved)  # slider滑动触发
        self.setMouseTracking(True)  # 在不点击鼠标的情况下也能获取鼠标移动信息

    def slider_moved(self, v: int):
        if self.mom.underMouse() and self.maximum() > 0:
            self.s_signal.emit(v / self.maximum())

    def set_slider_pos(self, p: float):
        # logger.debug(f"{self.mom.objectName()} set slider position to {p}")
        self.setSliderPosition(int(p * self.maximum()))

    def scroll_to_bottom(self):
        self.setSliderPosition(self.maximum())
